﻿@model IEnumerable<ByHand.Model.Product>
@{
    var tablename = "TableProduct";
    var numbercolumn = 5;
    var colsname = new[] { "Name", "Code", "Created", "End","Link FontEnd", "" };
    var curaction = "Index";
    var curcontroller = "Product";
    var configaction = new { details = "Details", del = "Del", create = "Create", undel = "UnDel", edit = "Edit" };
    var configtext = new { details = "Details", del = "Delete", create = "Create", undel = "Recovery", edit = "Edit" };
    var isfilter = ViewData["filter"] == null ? false : true;
}
<div class="render-content">
    <div class="form-search" id="form-search" style="text-align: center; padding-bottom: 20px;">
        <input type="text" class="input-medium search-query" value="@(isfilter ? ViewData["filter"] : "")" id="inputsearch" style="display:inline;height:30px;width:400px;"/>
        Nhập thông tin cần tìm và enter
    </div>
    <div class="content table-partial-content">
        <table class="table table-hover sort table-bordered">
            <thead>
                <tr>
                    @for (int i = 0; i < numbercolumn; i++)
                    {
                        <th>@colsname[i]
                            <span style="float: right;"><i class="icon-sort"></i></span>
                        </th>
                    }
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @item.Title
                        </td>
                        <td>
                            @item.ProductCode
                        </td>
                        <td>
                            @item.CreatedDate
                        </td>
                        <td>
                            @item.EndDate
                        </td>
                        <td>
                            <a href="@(SmallJob.Services.Code.StringHanding.CreateUrlProduct(item.ID,item.Title,item.Provider.Name))"> Link FontEnd </a>
                        </td>
                        <td>
                            @if (!item.IsActive)
                            {
                                <a href="javascript:;" onclick="active(@item.ID)">Active </a> @("|")
                            }
                           <a href="javascript:;" onclick="calldetailmodal(@item.ID);">@configtext.details</a>|
                        @Html.ActionLink(@configtext.edit, configaction.edit, new { id = item.ID }) |
                        @if (item.IsDeleted)
                        {
                            <a onclick="undel(this,@item.ID)" href="javascript:;">@configtext.undel</a> @("|")
                        }
                        else
                        {
                            <a  onclick="del(this,@item.ID)" href="javascript:;">@configtext.del</a> @("|")
                        }
                            <a href="@Url.Action("Option", "Product", new { id = item.ID })" >Option </a>
                            
                        </td>
                    </tr>
                }
            </tbody>
        </table>
        <div class="pagination">
            <ul>
                @{
                    var total = Convert.ToInt32(ViewData["total"]);
                    var curpage = Convert.ToInt32(ViewData["curpage"]);
                    if (curpage > 1)
                    {
                    <li><a href="/@curcontroller/@curaction/pages/@(curpage - 1)/filter/@(isfilter ? ViewData["filter"] : "")">Prev</a></li>
                    }
                    for (int i = 1; i <= total; i++)
                    {
                        if (i != curpage)
                        {
                    <li><a href="/@curcontroller/@curaction/pages/@(i)/filter/@(isfilter ? ViewData["filter"] : "")">@i</a></li>
                        }
                        else
                        {
                    <li><a href="/@curcontroller/@curaction/pages/@(i)/filter/@(isfilter ? ViewData["filter"] : "")"><strong>@i</strong></a></li>
                        }

                    }
                    if (curpage < total)
                    {
                    <li><a href="/@curcontroller/@curaction/pages/@(curpage + 1)/filter/@(isfilter ? ViewData["filter"] : "")">Next</a></li>
                    }
                }
            </ul>
        </div>
    </div>
    <div id="show-detail" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
        <div class="modal-body" style="max-height: none" id="div-content-detail"></div>
    </div>
</div>

<script src="@Url.Content("~/Content/js/jquery.tablesorter.min.js")"></script>
<script>
    //table sort
    $(function () {
        $("table.sort").tablesorter();
    });
    $("table").bind("sortStart", function () {
    }).bind("sortEnd", function () {
        $('th.header').each(function () {
            if ($(this).hasClass('headerSortDown')) {
                $(this).find('.icon-sort').addClass('icon-hand-up');
                $(this).find('.icon-sort').removeClass('icon-hand-down');

            }
            if ($(this).hasClass('headerSortUp')) {
                $(this).find('.icon-sort').addClass('icon-hand-down');
                $(this).find('.icon-sort').removeClass('icon-hand-up');
            }
            if (!$(this).hasClass('headerSortDown') && !$(this).hasClass('headerSortUp')) {
                $(this).find('.icon-sort').removeClass('icon-hand-down');
                $(this).find('.icon-sort').removeClass('icon-hand-up');
            }
        });
    });
    //del and undel ajax
    function del(source, id) {
        $.ajax({
            type: "POST",
            url: '@Url.Action(configaction.del, curcontroller)',
            data: { id: id },
            success: function (data) {
                if (data == true) {
                    $(source).text('@configtext.undel');
                    $(source).attr('onClick', 'undel(this,' + id + ')');
                } else {
                }
            }
        });
    }
    function undel(source, id) {
        $.ajax({
            type: "POST",
            url: '@Url.Action(@configaction.undel, curcontroller)',
            data: { id: id },
            success: function (data) {
                if (data == true) {
                    $(source).text('@configtext.del');
                    $(source).attr('onClick', 'del(this,' + id + ')');
                } else {
                }
            }
        });
    }
    function calldetailmodal(id) {
        $.ajax({
            type: "POST",
            url: '@Url.Action(@configaction.details, curcontroller)',
            data: { id: id },
            success: function (data) {
                if (data.length > 0) {
                    $('#show-detail').find('.modal-body').html(data);
                    $('#show-detail').modal();
                } else {
                }
            }
        });
    }
    $(document).ready(function () {
        $('#inputsearch').change(function () {
            var filter = $(this).val();
            $.ajax({
                type: "POST",
                url: '@Url.Action(@tablename, curcontroller)',
                data: { target: 1, page: 1, pagesize: 1, filter: filter },
                success: function (data) {
                    if (data != null) {
                        var getdata = $(data).find('.table-partial-content');
                        $('.table-partial-content').html(getdata.html());
                    } else {
                    }
                }
            });
        });
    });
    function active(id) {
        $.post('@Url.Action("Active", "Product")', { id: id }, function (d) {
            if (d) {
                window.location = window.location;
            }
        });
    }
</script>
